package cn.zz.comm.exception;

import java.util.Objects;

import org.springframework.lang.NonNull;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import lombok.Getter;

/**
 * @author zhongq
 * @since 2022/12/28
 */
@Getter
public class BizException extends RuntimeException {

	private String code;
	private String message;
	private Object data;

	public BizException(String message) {
		super(message);
	}

	public BizException(BizErrorCode errorCode, Object... params) {
		Objects.requireNonNull(errorCode);
		String message = errorCode.pattern();
		if (!ArrayUtil.isEmpty(params)) {
			message = StrUtil.format(errorCode.pattern(), params);
		}
		this.code = errorCode.code();
		this.message = message;
	}

	public static BizException ofData(@NonNull BizErrorCode errorCode, Object data, Object... params) {
		final BizException bizException = new BizException(errorCode, params);
		bizException.data = data;
		return bizException;
	}

}